<?php

namespace App\Http\Services;

use App\Models\BaseDetail;
use App\Models\Education;
use App\Models\JobColumn;
use App\Models\Project;
use App\Models\Recruit;
use App\Models\Skillcertification;
use App\Models\SkillTrain;
use App\Models\Work;
use App\Models\WorkExpect;
use Auth;
use Illuminate\Support\Facades\DB;

class ResumeService
{
    public function update($item)
    {
        $data = new BaseDetail();
        if(isset($item['id'])){
            $data = BaseDetail::find($item['id']);
            if(!$data){
                return false;
            }
        }

        $work_expect = WorkExpect::where('uid',$item['uid'])->first();
        if(!$work_expect){
            $work_expect = new WorkExpect();
        }
        
        $data->area = $item['area'];
        $data->birthday = strtotime($item['birthday']);
        $data->current_situation = $item['current_situation'];
        $data->education = $item['education'];
        $data->email = $item['email'];
        $data->head_img = $item['head_img'];
        $data->mobilephone = $item['mobilephone'];
        $data->name = $item['name'];
        $data->sex = $item['sex'];
        $data->update_time = time();
        $data->work_exp = $item['work_exp'];
        $data->workyear = $item['workyear'];

        
        
        $work_expect->address = $item['work_expect']['address'];
        $work_expect->flag = json_encode($item['work_expect']['flag']);
        $work_expect->industry = $item['work_expect']['industry'];
        $work_expect->job_type = $item['work_expect']['job_type'];
        $work_expect->position = $item['work_expect']['position'];
        $work_expect->resume_id = $item['work_expect']['resume_id'];
        $work_expect->salary = $item['work_expect']['salary'];
        $work_expect->salary_type = $item['work_expect']['salary_type'];
        
        
        try {
            DB::beginTransaction();
            $data->save();
            if(!$data->id){
                DB::rollBack();
                return false;
            }
            
            $work_expect->save();
            if(!$work_expect->id){
                DB::rollBack();
                return false;
            }
            DB::commit();
            return true;
        } catch (\Throwable $th) {
            throw $th;
            return  false;
        }
    }

    public function updateType($item)
    {
        $data = new JobColumn();
        if(isset($item['id']) && $item['id'] > 0){
            $data = JobColumn::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }
        $data->title = $item['title'];
        $data->torder = $item['torder'] ?? 0;
        $data->pid = $item['pid'] ?? 0;
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function workUpdate($item)
    {
        $data = new Work();
        if(isset($item['id']) && $item['id'] > 0){
            $data = Work::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
            $data->uid = $item['uid'];
            $data->resume_id = $item['resume_id'];
            $data->workfunc = '';
            $data->cdepartment = '';
            $data->worktype = 1;
            
            
        }

        $data->ccompname = $item['ccompname'];
        $data->cposition = $item['cposition'];
        $data->timefrom = $item['timefrom'];
        $data->timeto = $item['timeto'];
        $data->performance = $item['performance'];
        $data->workindustry = $item['workindustry'];
        $data->companysize = $item['companysize'];
        $data->companytype = $item['companytype'];
        $data->cworkdescribe = $item['cworkdescribe'];
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }


    public function projectUpdate($item)
    {
        $data = new Project();
        if(isset($item['id']) && $item['id'] > 0){
            $data = Project::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
            $data->uid = $item['uid'];
            $data->resume_id = $item['resume_id'] ?? 0;
            $data->img = $item['img'] ?? '';
            
        }
        $data->ccompname = $item['ccompname'];
        $data->cprojectname = $item['cprojectname'];
        $data->costs = $item['costs'];
        $data->post = $item['post'];
        $data->starttime = $item['starttime'];
        $data->endtime = $item['endtime'];
        $data->cdescribe = $item['cdescribe'];
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }


    public function eduUpdate($item)
    {
        $data = new Education();
        if(isset($item['id']) && $item['id'] > 0){
            $data = Education::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
            $data->uid = $item['uid'];
            $data->resume_id = $item['resume_id'] ?? 0;
            
        }
        $data->cschoolname = $item['cschoolname'];
        $data->cmajordes = $item['cmajordes'];
        $data->degree = $item['degree'];
        $data->cdescribe = $item['cdescribe'];
        $data->timefrom = $item['timefrom'];
        $data->timeto = $item['timeto'];
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function certUpdate($item)
    {
        $data = new Skillcertification();
        if(isset($item['id']) && $item['id'] > 0){
            $data = Skillcertification::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
            $data->uid = $item['uid'];
            $data->resume_id = $item['resume_id'] ?? 0;
            
        }
        $data->certname = $item['certname'];
        $data->getdate = $item['getdate'];
        $data->score = $item['score'] ?? '';
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function trainUpdate($item)
    {
        $data = new SkillTrain();
        if(isset($item['id']) && $item['id'] > 0){
            $data = SkillTrain::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
            $data->uid = $item['uid'];
            $data->resume_id = $item['resume_id'] ?? 0;
            
        }
        $data->cdescribe = $item['cdescribe'];
        $data->ctrainlesson = $item['ctrainlesson'];
        $data->ctrainname = $item['ctrainname'];
        $data->ctrainplace = $item['ctrainplace'];
        $data->timefrom = $item['timefrom'];
        $data->timeto = $item['timeto'];
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }
    
    
}
